﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DECOR.BL.BE;
using System.Collections.Generic;
using DECOR.BL.BC;
using Newtonsoft.Json;
using System.Collections.Specialized;

namespace DECOR.Touchapp
{
    public partial class verStock : System.Web.UI.Page
    {
        string pRetval = "";        
        string codMaterial = "";
        string centro = "";
        string almacen = "";
        string orgVentas = "";
        string canalDist = "";
        string textoStock = "";
        string textoStockEC = "";
        string Json = "";

        List<StockBE> lstStock = null;
        List<StockBE> lstStockEC = null;
        string fecImpStr = string.Empty; 
        string uMedida = string.Empty; 
        DateTime fecImp = new DateTime(); 
        decimal cantImp = 0m;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                
            }
            else
            {
                codMaterial = Request.Params["pCodMaterial"] == null ? "" : Request.Params["pCodMaterial"];
                //centro = Request.Params["pCentro"] == null ? "" : Request.Params["pCentro"];
                //almacen = Request.Params["pAlmacen"] == null ? "" : Request.Params["pAlmacen"];
                orgVentas = Request.Params["OrgVentas"] == null ? "" : Request.Params["OrgVentas"];
                canalDist = Request.Params["CanalDist"] == null ? "" : Request.Params["CanalDist"];

                pRetval = "{\"success\": {0}, \"errors\":{\"reason\": \"{1}\"}, \"lstStock\": {2}, ";
                pRetval += "\"textoStockEC\": \"{3}\", \"textoStock\": \"{4}\" ";
                pRetval += "}";

                CredencialesController.cargarInfo(Request.Params); //(SIEMPRE)
                
                cargarStock();

                Response.ContentType = "application/json; charset=UTF-8";
                Response.Write(pRetval);
            }
        }

        private void cargarStock()
        {
            StockBE objStock = new StockBE();
            objStock.CodMaterial = codMaterial;
            if (centro != string.Empty)
            {
                objStock.CodCentro = centro;
            }
            if (almacen != string.Empty)
            {
                objStock.CodAlmacen = almacen;
            }
            if (!orgVentas.Equals(string.Empty))
            {
                objStock.OrgVentas = orgVentas;
            }
            if (!canalDist.Equals(string.Empty))
            {
                objStock.CanalDist = canalDist;
            }

            lstStock = new List<StockBE>();
            lstStock = ParametroBC.Instance.getDataLotes(codMaterial, 0m, centro, almacen, "", "", "", orgVentas, canalDist);

            Json = JsonConvert.SerializeObject(lstStock);
            pRetval = pRetval.Replace("{0}", "true");
            pRetval = pRetval.Replace("{1}", "");
            pRetval = pRetval.Replace("{2}", Json);

            lstStockEC = new List<StockBE>();
            //lstStockEC = StockBC.Instance.buscarStockEC(objStock);

            foreach (StockBE obj in lstStockEC)
            {
                if (obj.FechaStock >= DateTime.Today)
                {
                    fecImp = obj.FechaStock;
                    cantImp = obj.StockDisponible;
                    uMedida = obj.UMB;
                    break;
                }
            }
            fecImpStr = string.Format("{0:dd/MM/yyyy}", fecImp);
            foreach (StockBE item in lstStock)
            {
                item.sStockDisponible = item.StockDisponible.ToString();
            }
            //Solo queda un nombre por centro
            for (int i = 0; i < lstStock.Count - 1; i++)
            {
                string DescCentro = lstStock[i].DescCentro;
                for (int j = i + 1; j <= lstStock.Count - 1; j++)//elimino los nombres de los centros repetidos
                {
                    if (DescCentro.Equals(lstStock[j].DescCentro))
                        lstStock[j].DescCentro = "";
                    else
                        break;
                }
                string DescAlmacen = lstStock[i].DescAlmacen;
                for (int j = i + 1; j <= lstStock.Count - 1; j++)//elimino los nombres de los Desc de Almacen repetidos
                {
                    if (DescAlmacen.Equals(lstStock[j].DescAlmacen) && lstStock[i].CodCentro.Equals(lstStock[j].CodCentro))
                        lstStock[j].DescAlmacen = "";
                    else
                        DescAlmacen = lstStock[j].DescAlmacen;
                }
            }

            if (lstStock.Count > 0)
            {
                string mDesc = lstStock[0].DescMaterial;
                decimal tDisp = lstStock.Sum(X => X.StockDisponible);

                if (cantImp > 0m)
                {
                    textoStockEC = string.Format("IMPORTACIÓN POR LLEGAR AL {0} : {1} {2}", fecImpStr, cantImp, uMedida);
                    pRetval = pRetval.Replace("{3}", textoStockEC);
                    
                }
                else
                {
                    pRetval = pRetval.Replace("{3}", "");    //borrar la descripcion, debe ser vacia
                }
                textoStock = "Stock : " + codMaterial + " - " + mDesc +  " - Disponible Total = " + tDisp.ToString();
                if (string.IsNullOrEmpty(textoStock))
                {
                    pRetval = pRetval.Replace("{4}", "");
                }
                else
                {
                    pRetval = pRetval.Replace("{4}", textoStock);
                }
            }
            else
            {
                string result = "No se encontró información para el material: " + codMaterial;
                pRetval = pRetval.Replace("{0}", "false");
                pRetval = pRetval.Replace("{1}", result);
                pRetval = pRetval.Replace("{2}", null);
                pRetval = pRetval.Replace("{3}", "");
                pRetval = pRetval.Replace("{4}", "");
            }
        }
    }
}
